import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入 store
import store from '@/store'

Vue.use(VueRouter)

// 路由规则（添加需要认证的 requiresAuth 信息）
const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: 'login' */'@/views/login/index')
  },
  {
    path: '/',
    component: () => import(/* webpackChunkName: 'layout' */'@/views/layout/index'),
    // 直接给某个路由设置，这时内部的子路由都需要认证（包含当前路由）
    meta: { requiresAuth: true },
    children: [
      // 课程管理，管理员和教师展示不同数据
      {
        path: '',
        name: 'course-manager',
        component: () => import(/* webpackChunkName: 'role' */'@/views/courseManager/index'),
        meta: { requiresAuth: true, role: 'both' },
      },
      // 添加课程组件
      {
        path: '/course/create',
        name: 'course-create',
        component: () => import(/* webpackChunkName: 'course-create' */'@/views/courseManager/create'),
        meta: { requiresAuth: true, role: 'both' },
      },
      // 编辑课程组件
      {
        path: '/course/:courseId/edit',
        name: 'course-edit',
        component: () => import(/* webpackChunkName: 'course-edit' */'@/views/courseManager/edit'),
        props: true,
        meta: { requiresAuth: true, role: 'both' },
      },
      // 课程内容组件
      {
        path: '/course/:courseId/section',
        name: 'course-section',
        component: () => import(/* webpackChunkName: 'course-section' */'@/views/courseManager/section'),
        props: true,
        meta: { requiresAuth: true, role: 'both' },
      },
      // 上传课时视频
      {
        path: '/course/:courseId/video',
        name: 'course-video',
        component: () => import(/* webpackChunkName: 'course-video' */'@/views/courseManager/video'),
        props: true,
        meta: { requiresAuth: true, role: 'both' },
      },
      // 用户管理，仅管理员可见
      {
        path: '/user-manager',
        name: 'user-manager',
        component: () => import(/* webpackChunkName: 'role' */'@/views/userManager/index'),
        meta: { requiresAuth: true, role: 'admin' },
      },
      ,
      // 用户管理，仅管理员可见
      {
        path: '/student-manager',
        name: 'student-manager',
        component: () => import(/* webpackChunkName: 'role' */'@/views/studentManager/index'),
        meta: { requiresAuth: true, role: 'admin' },
      },
      // 销售统计，管理员和教师展示不同数据
      {
        path: '/sales-statistics',
        name: 'sales-statistics',
        component: () => import(/* webpackChunkName: 'menu' */'@/views/salesStatistics/index'),
        meta: { requiresAuth: true, role: 'both' },
      },
      // 广告课程管理，仅管理员可见
      {
        path: '/adv-course',
        name: 'adv-course',
        component: () => import(/* webpackChunkName: 'resource' */'@/views/advCourse/index'),
        meta: { requiresAuth: true, role: 'admin' },
      },
      {
        path: '/my-info',
        name: 'my-info',
        component: () => import(/* webpackChunkName: 'resource' */'@/views/myInfo/index'),
        meta: { requiresAuth: true, role: 'both' },
      },
      {
        path: '/test',
        name: 'test',
        component: () => import(/* webpackChunkName: 'resource' */'@/views/test/index'),
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 验证 to 路由是否需要进行身份认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    let role = store.state.userRole;
    // 判断是否登录且用户角色是否有访问权限
    if (store.state.user && to.matched.every(record => record.meta.role == 'admin' ? role == 'admin' : true)) {
      next()
    } else {
      return next({
        name: 'login',
        query: {
          // 将本次路由的 fullPath 传递给 login 页面
          redirect: to.fullPath
        }
      })
    }
  } else {
    next()
  }
})

export default router
